package com.fr.chart.core.glyph;

import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.DateUtils;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.axis.Axis;
import com.fr.chart.axis.CategoryAxis;
import com.fr.chart.base.equals.Equals;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Stroke;
import java.awt.geom.Line2D;
import java.awt.geom.Line2D.Double;
import java.text.Format;
import java.util.ArrayList;

public class CategoryAxisGlyph extends AxisGlyph
{
  private static final long serialVersionUID = -6116809732597680006L;
  public static final String XML_TAG = "CategoryAxisGlyph";
  protected ArrayList categoryLabelList = new ArrayList();
  protected CategoryAxis categoryAxis;
  private boolean isGantt = false;
  protected int forward = 0;
  protected int backward = 0;

  public CategoryAxisGlyph()
  {
  }

  public CategoryAxisGlyph(CategoryAxis paramCategoryAxis)
  {
    super(paramCategoryAxis);
  }

  public void calculateAxisLengthUnit()
  {
    if (getCategoryFullCount() - getCrossValue() == 0.0D)
      this.unitLength = 0.0D;
    else
      this.unitLength = (this.axisLength / (getCategoryFullCount() - getCrossValue()));
    this.tickLength = 1.0D;
    this.smallTickLength = (this.tickLength / 5.0D);
  }

  public int getCategoryCount()
  {
    return this.categoryLabelList.size();
  }

  public int getCategoryFullCount()
  {
    return (this.categoryLabelList.size() + this.forward);
  }

  public Axis getAxis()
  {
    return this.categoryAxis;
  }

  public void setAxis(Axis paramAxis)
  {
    if (!(paramAxis instanceof CategoryAxis))
      throw new IllegalArgumentException();
    this.categoryAxis = ((CategoryAxis)paramAxis);
  }

  public void setGantt(boolean paramBoolean)
  {
    this.isGantt = paramBoolean;
  }

  public boolean isGantt()
  {
    return this.isGantt;
  }

  public void setForward(int paramInt)
  {
    this.forward = paramInt;
  }

  public void setBackward(int paramInt)
  {
    this.backward = paramInt;
  }

  public void addCategoryLabel(Object paramObject)
  {
    this.categoryLabelList.add(paramObject);
  }

  public ArrayList getCategoryLabelList()
  {
    return this.categoryLabelList;
  }

  public void setCategoryLabelList(ArrayList paramArrayList)
  {
    this.categoryLabelList = paramArrayList;
  }

  protected void drawAxisGrid(Graphics paramGraphics)
  {
    Stroke localStroke;
    Paint localPaint;
    double d;
    Line2D[] arrayOfLine2D;
    int i;
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    if ((getAxis().getSecondGridStyle() != 0) && (this.smallTickLength > 0.0D))
    {
      localStroke = localGraphics2D.getStroke();
      localPaint = localGraphics2D.getPaint();
      localGraphics2D.setStroke(GraphHelper.getStroke(getAxis().getSecondGridStyle()));
      localGraphics2D.setPaint(getAxis().getSecondGridColor());
      d = this.smallTickLength + getCrossValue();
      while (d <= getCategoryFullCount())
      {
        arrayOfLine2D = getGridLine(d);
        for (i = 0; i < arrayOfLine2D.length; ++i)
          localGraphics2D.draw(arrayOfLine2D[i]);
        d += this.smallTickLength;
      }
      localGraphics2D.setPaint(localPaint);
      localGraphics2D.setStroke(localStroke);
    }
    if ((getAxis().getMainGridStyle() != 0) && (this.tickLength > 0.0D))
    {
      localStroke = localGraphics2D.getStroke();
      localPaint = localGraphics2D.getPaint();
      localGraphics2D.setStroke(GraphHelper.getStroke(getAxis().getMainGridStyle()));
      localGraphics2D.setPaint(getAxis().getMainGridColor());
      d = this.tickLength + getCrossValue();
      while (d <= getCategoryFullCount())
      {
        arrayOfLine2D = getGridLine(d);
        for (i = 0; i < arrayOfLine2D.length; ++i)
          localGraphics2D.draw(arrayOfLine2D[i]);
        d += this.tickLength;
      }
      localGraphics2D.setStroke(localStroke);
      localGraphics2D.setPaint(localPaint);
    }
  }

  protected void drawAxisLine(Graphics paramGraphics)
  {
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    Paint localPaint = localGraphics2D.getPaint();
    Color localColor = getAxis().getLineColor();
    int i = getAxis().getLineStyle();
    if ((localColor != null) && (i != 0) && (getAxis().isAxisShow()))
    {
      localGraphics2D.setPaint(localColor);
      Line2D.Double localDouble = new Line2D.Double(getPoint2DForSelf(getCrossValue()), getPoint2DForSelf(getCategoryFullCount()));
      GraphHelper.draw(localGraphics2D, localDouble, i);
      drawArrow(localGraphics2D, getPoint2DForSelf(getCrossValue()), getPoint2DForSelf(getCategoryFullCount()));
    }
    drawTicks(localGraphics2D);
    localGraphics2D.setPaint(localPaint);
  }

  public void drawTicks(Graphics paramGraphics)
  {
    Object localObject1;
    double d1 = getCategoryFullCount();
    BasicStroke localBasicStroke1 = new BasicStroke(this.categoryAxis.getMainStroke(), 1, 0);
    BasicStroke localBasicStroke2 = new BasicStroke(this.categoryAxis.getSecStroke(), 1, 0);
    double d2 = getCrossValue() + this.tickLength;
    while ((d2 <= d1) && (getAxis().isAxisShow()))
    {
      localObject1 = getTickLine(d2, this.categoryAxis.getTickLength(), this.categoryAxis.getTickMarkType());
      if (this.categoryAxis.getTickLength() != 0)
      {
        if (this.categoryAxis.getMainStroke() == 0.0F)
          break label126:
        drawTickLine(paramGraphics, (Line2D)localObject1, localBasicStroke1);
      }
      label126: d2 += this.tickLength;
    }
    d2 = getCrossValue() + this.smallTickLength;
    while ((d2 <= d1) && (getAxis().isAxisShow()))
    {
      if ((d2 - getCrossValue()) / this.smallTickLength == 5.0D)
        break label244:
      localObject1 = getTickLine(d2, this.categoryAxis.getSecTickLength(), this.categoryAxis.getSecTickMarkType());
      if (this.categoryAxis.getSecTickLength() != 0)
      {
        if (this.categoryAxis.getSecStroke() == 0.0F)
          break label244:
        drawTickLine(paramGraphics, (Line2D)localObject1, localBasicStroke2);
      }
      label244: d2 += this.smallTickLength;
    }
    int i = 1;
    for (int j = 0; j < this.categoryLabelList.size(); ++j)
    {
      localObject1 = this.categoryLabelList.toArray();
      for (l = 0; l < localObject1[j].toString().length(); ++l)
        if (!(Character.isDigit(localObject1[j].toString().charAt(l))))
        {
          i = 0;
          break;
        }
      if (i == 0)
        break;
    }
    Format localFormat = getAxis().getFormat();
    int k = 0;
    int l = 0;
    if ((getAxis().getLabelNumber() == -1) || (getAxis().getLabelNumber() == 0))
      l = 1;
    else
      l = getAxis().getLabelNumber();
    int i1 = 0;
    while (i1 < this.categoryLabelList.size())
    {
      int i2;
      if (localFormat != null)
      {
        Object localObject2;
        Object[] arrayOfObject = this.categoryLabelList.toArray();
        if (i != 0)
        {
          localObject2 = localFormat.format(Double.valueOf(arrayOfObject[i1].toString()));
          if (getAxis().isStagger())
            drawBGLabel(paramGraphics, i1, 0.5D, (String)localObject2, k);
          else
            drawLabel(paramGraphics, i1, (String)localObject2);
        }
        else
        {
          localObject2 = DateUtils.object2Date(this.categoryLabelList.get(i1), true);
          String str = (localObject2 == null) ? this.categoryLabelList.get(i1).toString() : localFormat.format(localObject2);
          if (getAxis().isStagger())
            drawBGLabel(paramGraphics, i1, 0.5D, str, k);
          else
            drawLabel(paramGraphics, i1, str);
        }
      }
      else if (getAxis().isStagger())
      {
        if (this.isGantt)
          drawBGLabel(paramGraphics, i1, 0.5D, this.categoryLabelList.get(getCategoryCount() - i1 - 1).toString(), k);
        else
          drawBGLabel(paramGraphics, i1, 0.5D, this.categoryLabelList.get(i1).toString(), k);
      }
      else if (this.isGantt)
      {
        drawLabel(paramGraphics, i1, this.categoryLabelList.get(getCategoryCount() - i1 - 1).toString());
      }
      else
      {
        drawLabel(paramGraphics, i1, this.categoryLabelList.get(i1).toString());
      }
      ++k;
      i1 += l;
    }
  }

  public void axisExtends(int paramInt1, int paramInt2)
  {
    setForward(paramInt1);
    setBackward(paramInt2);
    calculateAxisLengthUnit();
  }

  public double getCrossValue()
  {
    return (0 - this.backward);
  }

  protected void drawLabel(Graphics paramGraphics, double paramDouble, String paramString)
  {
    drawLabel(paramGraphics, paramDouble, 0.5D, paramString);
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      if (str1.equals("LabelList"))
      {
        paramXMLableReader.readXMLObject(new XMLReadable(this)
        {
          private final CategoryAxisGlyph this$0;

          public void readXML()
          {
            if ((paramXMLableReader.isChildNode()) && ("Label".equals(paramXMLableReader.getTagName())))
            {
              String str;
              if ((str = paramXMLableReader.getAttr("value")) != null)
                this.this$0.categoryLabelList.add(str);
            }
          }
        });
      }
      else
      {
        String str2;
        if ((str1.equals("CategoryAxis")) || (str1.equals("DateAxis")))
          if (((str2 = paramXMLableReader.getAttr("class")) != null) && (str2.endsWith(".CategoryAxis")))
            this.categoryAxis = ((CategoryAxis)paramXMLableReader.readXMLObject(new CategoryAxis()));
        else if (str1.equals("attr"))
          if ((str2 = paramXMLableReader.getAttr("isGantt")) != null)
            setGantt(Boolean.valueOf(str2).booleanValue());
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("CategoryAxisGlyph");
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("attr").attr("isGantt", this.isGantt).end();
    if ((this.categoryLabelList != null) && (this.categoryLabelList.size() > 0))
    {
      paramXMLPrintWriter.startTAG("LabelList");
      for (int i = 0; i < this.categoryLabelList.size(); ++i)
        paramXMLPrintWriter.startTAG("Label").attr("value", this.categoryLabelList.get(i).toString()).end();
      paramXMLPrintWriter.end();
    }
    if (this.categoryAxis != null)
      this.categoryAxis.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof CategoryAxisGlyph))
      return false;
    CategoryAxisGlyph localCategoryAxisGlyph = (CategoryAxisGlyph)paramObject;
    if (!(super.equals(localCategoryAxisGlyph)))
      return false;
    if (!(ComparatorUtils.equals(localCategoryAxisGlyph.categoryLabelList, this.categoryLabelList)))
      return false;
    if (!(Equals.equals(localCategoryAxisGlyph.getAxis(), getAxis())))
      return false;
    return (this.isGantt == localCategoryAxisGlyph.isGantt);
  }

  public JSONObject toJSONObject()
    throws JSONException
  {
    JSONObject localJSONObject = super.toJSONObject();
    if (this.categoryAxis != null)
      localJSONObject.put("categoryAxis", this.categoryAxis.toJSONObject());
    if ((this.categoryLabelList != null) && (this.categoryLabelList.size() > 0))
      localJSONObject.put("categoryLabelList", this.categoryLabelList.toArray());
    localJSONObject.put("isGantt", this.isGantt);
    localJSONObject.put("forward", this.forward);
    localJSONObject.put("backward", this.backward);
    return localJSONObject;
  }

  public String getJSAxisType()
  {
    return "CategoryAxisGlyph";
  }
}